﻿using HandyControl.Tools.Command;
using HMI.Model;
using HMI.Pojo;
using HMI.Tool;
using Prism.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WLMes.Models;

namespace HMI.View.OrderFrmTools
{
    /// <summary>
    /// OrderItem.xaml 的交互逻辑
    /// </summary>
    public partial class OrderItem : UserControl
    {
        public OrderItem()
        {
            InitializeComponent();
            this.DataContext = new OrderItemViewModel();
        }
        public Action BackEvent { get; set; }
        SqlSugarHelper sql  = new SqlSugarHelper();
        internal void FillData(string id, string batch)
        {
            List<TaskSetsDTO> sets =sql.db.Queryable<TaskSetsDTO>().Where(t=>t.TaskSets_ID==id).ToList();
            // 处理字段并赋值
            List<string> lisID = new List<string>();
            List<string> partsID = new List<string>();
            List<string> parasID = new List<string>();
            foreach (var set in sets)
            {
                
                TaskSetsVO vO = new TaskSetsVO();
                vO.TaskNumber = set.TaskNumber;
                vO.SapBatch = set.SAPBatchNumber;
                vO.Catagory = set.PartCategories;
                vO.MatCode = set.PartMatCode;
                vO.MatDescription = set.PartMatDescription;
                int qty = set.Quantity;
                int qtyx = set.CompleteQty;
                vO.Status = qty - qtyx > 0 ? "未完成" : "完成";
                vO.OrderQty = qty.ToString();
                vO.DoneQty = qtyx.ToString();
                vO.ExpQty = "0";
                vO.OrderTime = set.tasksetsUserTime;
                gd.Items.Add(vO);
                lisID.Add(set.MaterialSets_ID);
                partsID.Add(set.PartSets_ID);
                parasID.Add(set.ParameterSets_ID);
            }
            List<PartSets_ListsDTO> partDTO =sql.db.Queryable<PartSets_ListsDTO>()
                .In(o=>o.PartSets_Lists_ID,partsID).ToList();
            foreach (var part in partDTO) {
                PartSets_ListsVO pvo = new PartSets_ListsVO();
                pvo.SerialNumber = part.PartSerialNumber;
                pvo.OnlineTime = part.OnlineTime;
                pvo.OffLineTime = part.CompleteTime;
                pvo.Status = part.CompleteTime == "" ? "未完成" : "完成";
                dgpart.Items.Add(pvo);
            }
            List<MaterialSetsDTO> mdto = sql.db.Queryable<MaterialSetsDTO>()
                .In(o => o.MaterialSets_ID, lisID).ToList();
            foreach (var md in mdto)
            {
                MaterialSetsVO mvo =new MaterialSetsVO();
                mvo.Seq = md.MaterialSequenceNumber.ToString();
                mvo.MatCode = md.MaterialCode;
                mvo.MatDescription = md.Description;
                mvo.CompenentSize = md .SIZE;
                mvo.MatQty = md.Quantity.ToString();
                mvo.MatUnit = md.Unit;
                dgmat.Items.Add(mvo);
            }
            List<ParameterSetsDTO> padto = sql.db.Queryable<ParameterSetsDTO>()
                .In(o => o.ParameterSets_ID, parasID).ToList();
            foreach (var pad in padto) {
                ParameterSetsVO pavo =new  ParameterSetsVO();
                pavo.Seq = pad.ParameterSequenceNumber.ToString();
                pavo.ParaCode = pad.ParameterCode;
                pavo.ParaName = pad.ParameterName;
                pavo.ParaValue = pad.ParameterValue;
                dgparas.Items.Add(pavo);
            }
        }

        private void paras_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
        {
            DataGrid datagrid = sender as DataGrid;
            datagrid.UnselectAllCells();

        }
    }
}
